package com.first.common.util;

import com.aliyun.tea.TeaException;
import org.springframework.stereotype.Component;

import java.util.Random;


/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-06-05 14:32
 **/
@Component
public class VerificationUtil {
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tHUm1DtDYuKvxhHkg3e")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("3gVauuyo8BJozypLFJkwa11FV6quEx");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
        public static String getCode(String mobile){
            String c = getC();
            com.aliyun.dysmsapi20170525.Client client = null;
            try {
                client = VerificationUtil.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName("小霸王学习登录")
                    .setTemplateCode("SMS_460960684")
                    .setPhoneNumbers(mobile)
                    .setTemplateParam("{\"code\":\""+c+"\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, runtime);
            } catch (TeaException error) {
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
            }
            return c;
        }
    private static String getC(){
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000));
    }
}
